package com.xiaozhi.java.ai.langchain4j.controller;

import com.xiaozhi.java.ai.langchain4j.assistant.XiaozhiAgentMCP;
import com.xiaozhi.java.ai.langchain4j.bean.ChatForm;
import com.xiaozhi.java.ai.langchain4j.tools.AppointmentTools;
import com.xiaozhi.java.ai.langchain4j.tools.BailianTool;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@Tag(name = "硅谷小智MCP版V2")
@RestController
@RequestMapping("/xiaozhi-mcp2")
public class XiaozhiMCPV2Controller {

    @Autowired
    private QwenStreamingChatModel qwenStreamingChatModel;

    @Autowired
    private ChatMemoryProvider chatMemoryProviderXiaozhi;

    @Autowired
    private AppointmentTools appointmentTools;

    @Autowired
    private ContentRetriever contentRetrieverXiaozhiPincone;

    @Autowired
    private BailianTool bailianTool; // 注入百炼工具

    @Operation(summary = "对话")
    @PostMapping(value = "/chat2", produces = "text/stream;charset=utf-8")
    public Flux<String> chat(@RequestBody ChatForm chatForm) {
        // 创建集成了百炼工具的Agent
        XiaozhiAgentMCP xiaozhiAgentMCP = createEnhancedAgent();
        return xiaozhiAgentMCP.chat(chatForm.getMemoryId(), chatForm.getMessage());
    }

    private XiaozhiAgentMCP createEnhancedAgent() {
        return AiServices.builder(XiaozhiAgentMCP.class)
                .streamingChatLanguageModel(qwenStreamingChatModel)
                .chatMemoryProvider(chatMemoryProviderXiaozhi)
                .tools(appointmentTools, bailianTool) // 集成百炼工具
                .contentRetriever(contentRetrieverXiaozhiPincone)
                .build();
    }
}